#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: api01_connections.py 
@version:
@time: 2022/03/01 
"""
from TapClientCore.T02_tm_api.v2_api.v2_api_base import *


class Api01ConnectionsUrl(V2ApiUrlBase):
    url_connections = api_joint(V2ApiUrlBase().server, CONNECTIONS)
    url_database_type = api_joint(V2ApiUrlBase().server, DATABASE_TYPES)
    url_connections_list_all = api_joint(url_connections, CONNECTIONS_LIST_ALL)

    def __str__(self):
        response = {
            self.url_connections: '',
        }
        return response.__str__()


class Api01Connections(V2ApiBase):

    def get_connections_custom_query(self, connections_id):
        """
        customQuery
        :param connections_id:
        :return:
        """
        response = self.tapclient(
            self.get,
            api_joint(
                Api01ConnectionsUrl.url_connections,
                connections_id,
                CUSTOMQUERY
            )
            + '?schema=True'
            + self.access_token2
        )
        return response

    def get_connections_database_type(self):
        """
        查询数据库类型
        :return:
        """
        response = self.tapclient(
            self.get,
            Api01ConnectionsUrl.url_database_type + self.access_token
        )
        return response

    def get_connections(self, limit=500):
        """
        查询数据源
        :param limit:
        :return:
        """
        response = self.tapclient(
            self.get,
            Api01ConnectionsUrl.url_connections + self.access_token + self.filter({"limit": limit})
        )
        return response

    def post_connections(self, payload):
        """
        添加数据源
        :param payload:
        :return:
        """
        response = self.tapclient(
            self.post,
            Api01ConnectionsUrl.url_connections + self.access_token,
            json=payload
        )
        return response

    def patch_connections_by_id(self, connections_id, payload):
        """
        通过id修改数据源
        :param connections_id:
        :param payload:
        :return:
        """
        response = self.tapclient(
            self.patch,
            api_joint(Api01ConnectionsUrl.url_connections, connections_id)
            + self.access_token,
            json=payload
        )
        return response

    def delete_connections_by_id(self, connections_id):
        """
        通过id删除数据源
        :param connections_id:
        :return:
        """
        response = self.tapclient(
            self.delete,
            api_joint(Api01ConnectionsUrl.url_connections, connections_id)
            + self.access_token,
        )
        return response

    def get_connections_by_id(self, connections_id):
        """
        通过id获取数据源
        :param connections_id:
        :return:
        """
        response = self.tapclient(
            self.get,
            api_joint(Api01ConnectionsUrl.url_connections, connections_id)
            + self.access_token,
        )
        return response

    def post_connections_copy_by_id(self, connections_id, payload):
        """
        通过id和payload copy数据源
        :param connections_id:
        :param payload: {"uri":"621e027d98bcdf1ea2e2e0ee/copy","headers":{"lconname-name":"auto_mysql"}}
        :return:
        """
        response = self.tapclient(
            self.post,
            api_joint(Api01ConnectionsUrl.url_connections, connections_id, CONNECTIONS_COPY)
            + self.access_token,
            json=payload
        )
        return response

    def get_connections_listAll(self):
        response = self.tapclient(
            self.get,
            Api01ConnectionsUrl.url_connections_list_all
            + self.access_token
            + self.filter({
                "fields": {
                    "id": True,
                    "name": True,
                    "connection_type": True,
                    "status": True,
                    "user_id": True
                },
                "where": {
                    "or": [
                        {
                            "connection_type": "source_and_target"
                        },
                        {
                            "connection_type": "target"
                        }
                    ],
                    "database_type": {
                        "in": [
                            "mongodb",
                            "gridfs",
                            "oracle",
                            "mysql",
                            "sqlserver",
                            "postgres"
                        ]
                    }
                }
            })
        )
        return response
